package com.wj.opratorlog.controller;

import com.google.common.collect.*;
import com.sun.net.httpserver.Filter;
import com.wj.opratorlog.entity.UerInfo;
import com.wj.opratorlog.pojo.OperationLogReq;
import com.wj.opratorlog.service.OperatorService;
import com.wj.opratorlog.utils.MessageUtils;
import com.wj.opratorlog.utils.ResultResPonse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.MessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/operator")
@Slf4j
public class OperatorController {
    @Autowired
    private OperatorService operatorService;



    public ResultResPonse<?> saveLog(@RequestBody OperationLogReq operationLogReq) {

        HashMap<String,List<String>> s= new HashMap();

        operatorService.saveLog(operationLogReq);
        return ResultResPonse.ok();
    }

    @RequestMapping(value = "/aysTask", method = {RequestMethod.GET})
    public ResultResPonse<?> AysTask() throws ExecutionException, InterruptedException {

        HashMultimap<String, String> objectObjectHashMultimap = HashMultimap.create();
        objectObjectHashMultimap.put("goup111","111" );
        objectObjectHashMultimap.put("goup111","3333" );
        objectObjectHashMultimap.put("goup114444","3333" );
        objectObjectHashMultimap.put("goup114446","8" );
        Set<String> goup111 = objectObjectHashMultimap.get("goup111");

        TreeSet<Comparable> comparables = Sets.newTreeSet();

//        log.info("###############################");
//        CompletableFuture<List<String>> taskOne = CompletableFuture.supplyAsync( () -> {
//            List<String> admin = List.of("admin", "stest", "xiafengegeg");
//
//            log.info("task one ......");
//            return admin;
//        });
//
//        CompletableFuture<List<String>> task2= CompletableFuture.supplyAsync( () -> {
//            List<String> admin = List.of("xiaoming", "xiaosan", "zhanglan");
//            log.info("task twone ......");
//
//            return admin;
//        });
//
//        CompletableFuture<List<String>> task3= CompletableFuture.supplyAsync( () -> {
//            List<String> admin = List.of("wangsongwen", "xiaoyagne", "duling");
//            log.info("task threee ......");
//
//            return admin;
//        });
//         CompletableFuture.allOf(taskOne,task2,task3).join();
//         taskOne.thenCombine(task2, (admin1,admin2) -> {});


        CompletableFuture<String> completedFuture = CompletableFuture.completedFuture("Hello World");

        String join = completedFuture.join();


        return ResultResPonse.ok(join);
    }

    @GetMapping("/getAsyscValue")
    public ResultResPonse<?> getAsyscValue(String query) throws ExecutionException, InterruptedException {
        MDC.put("userId", "123456");
        String value = query;
        InheritableThreadLocal<String> sss = new InheritableThreadLocal<>();
        sss.set("ppppppp");
        String name = "sssfefwef";

        new Thread(() -> {
            log.info("name====:" + name);
        }).start();


        CompletableFuture<String> stringCompletableFuture = operatorService.execute(value);

        log.info("======:value" + MDC.get("userId"));


        Map<Integer, Integer> maps = maps(new int[]{1, 2, 2, 3, 3, 3, 4, 5, 5});
        log.info("maps:=========" + maps);
        ClassPathResource classPathResource = new ClassPathResource("i18n/login_en_US.properties");

        ResourceBundle rb1 = ResourceBundle.getBundle("i18n/login", Locale.US);
        ResourceBundle rb2 = ResourceBundle.getBundle("i18n/login", Locale.CHINA);

        String name1 = MessageUtils.get("name", Arrays.asList("222222222").toArray());


        System.out.println("us:" +  name1 );
        System.out.println("cn:" + rb2.getString("name"));


        return ResultResPonse.ok(stringCompletableFuture.get());

    }

    private List<Integer> findNums(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        List findNums = Lists.newArrayList();

        for (int i = 0; i < nums.length; i++) {
            if (map.get(nums[i]) == null) {
                map.put(nums[i], 0);
            } else {
                map.put(Integer.valueOf(String.valueOf(nums[i])), map.get(nums[i]) + 1);
            }
        }
        for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
            if (integerIntegerEntry.getValue() == 1) {
                findNums.add((integerIntegerEntry.getKey()));
            }
        }
        return findNums;
    }


    public Map<Integer, Integer> maps(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (map.get(nums[i]) == null) {
                map.put(nums[i], 0);
            } else {
                map.put(Integer.valueOf(String.valueOf(nums[i])), map.get(nums[i]) + 1);
            }
        }

        return map;
    }


}
